package eu.orangeflash.webapis.wp
{
	import flash.events.*;
	
	import eu.orangeflash.webapis.events.XMLRPCEvent;
	import eu.orangeflash.webapis.xmlrpc.*;

	public class WP
	extends Client
	implements IClient
	{
		protected var userData:Credentials;
		
		public function WP(credentials:Credentials)
		{
			userData = credentials;
		}
		
		public override function set serviceURL(value:String):void
		{
			//we set it once in constructor, this is not used.
		}
		public override function get serviceURL():String
		{
			return userData.serviceURL;
		}
		
		public override function invoke(methodName:String, ...args):IMethodCall
		{
			var result:WPMethod = new WPMethod(this);
				result.addEventListener(XMLRPCEvent.RESULT, onResult);
				result.addEventListener(XMLRPCEvent.FAULT, onFault);
				result.invoke(methodName,args);
			
			return result;
		}
		
		public function editPost(postID:String, content:Content, publish:Boolean):WPMethod
		{
			return invoke(
							Methods.EDIT_POST,
							postID,
							userData.userName,
							userData.password,
							content.rawStruct,
							publish) as WPMethod;
		}
		
		public function newPost(content:Content, publish:Boolean):WPMethod
		{
			return invoke(
							Methods.NEW_POST,
							userData.blogId,
							userData.userName,
							userData.password,
							content.rawStruct,
							publish
						 ) as WPMethod;
		}
		
		public function getPost(postID:String):WPMethod
		{
			return invoke(
							Methods.GET_POST,
							postID,
							userData.userName,
							userData.password
						 ) as WPMethod;
		}
		
	}
}